#!/usr/bin/ruby
# -*- ruby -*-
#
# http://www.reddit.com/r/dailyprogrammer/comments/12k3xt/1132012_challenge_110_intermediate_creepy_crawlies/

require 'rexml/document'
require 'rexml/xpath'

url = ARGV.shift || 'http://www.reddit.com/r/nosleep/top/.rss'

xml = `curl #{url} 2> /dev/null` || Kernel.exit(-1)

d = REXML::Document.new(xml)

d.elements.each("rss/channel/item") do |i|
    title = nil
    desc  = nil

    i.elements.each do |e|
        if e.name == 'title'
            title = e.text
        elsif e.name == 'description'
            desc = e.text
        end
    end

    puts "=== #{title} ==="
    puts
    puts desc
    puts
end
